<template>
  <p-drawer
    placement="right"
    :closable="false"
    width="calc(100vw - 200px)"
    :visible="visible"
    :mask-closable="false"
    :destroy-on-close="true"
    @close="onClose"
  >
    <TaskTitle :title-name="titleName" icon-name="Det" color="green" />
    <div class="g-drawer-list-container">
      <div class="g-drawer-progress-container">
        <TitleSecondary title="审批流程图" />
        <ProgressStaus />
      </div>
      <ProgressTransverse
        :node-list="detail.nodeList"
        :name-list="detail.nameList"
      />
      <TitleSecondary title="基本信息" />
      <FormBasicInfoList :form="form" :plan-type="planType" />
      <TitleSecondary title="销售责任人" />
      <TableSaleList
        :data-source="detail.planSales"
        style="margin-bottom: 20px"
      />
      <TitleSecondary :title="`${PLAN_TYPE[planType]}计划明细`" />

      <p-tabs
        default-active-key="2"
        class="g-drawer-poros-tabs"
        type="card"
        @change="handleTabChange"
      >
        <p-tab-pane key="1" :tab="`初版${PLAN_TYPE[planType]}目标`">
          <TablePlanDetail
            ref="table-detail"
            :data-source="planDetailList"
            :is-edit="false"
            :hide-keys="['selection']"
            :plan-type="planType"
            :plan-sales-id="detail.planSalesId"
            :plan-id="planId"
            :plan-sales="detail.planSales"
            :detail-copy="detailStartCopy"
            :local-key="`${PLAN_TYPE[planType]}-plan-end`"
            :buttons="[
              {
                key: 'export',
                label: '导出',
              },
              {
                key: 'show',
                label: '显示字段',
              },
              {
                key: 'screenfull',
                label: '全屏显示',
              },
            ]"
            :time-range="[form.startTime, form.endTime]"
            :parent-id="form.salesOrganization"
            @checked="handleTableCheckedChange"
          />
        </p-tab-pane>
        <p-tab-pane key="2" :tab="`终版${PLAN_TYPE[planType]}目标`">
          <TablePlanDetail
            ref="table-detail"
            :data-source="endPlanDetailList"
            :detail-copy="detailEndCopy"
            :is-edit="false"
            template-name="终版"
            :plan-sales="detail.planSales"
            :hide-keys="['selection']"
            :plan-type="planType"
            :local-key="`${PLAN_TYPE[planType]}-plan-end-end`"
            :buttons="[
              {
                key: 'export',
                label: '导出',
              },
              {
                key: 'show',
                label: '显示字段',
              },
              {
                key: 'screenfull',
                label: '全屏显示',
              },
            ]"
            :time-range="[form.startTime, form.endTime]"
            :parent-id="form.salesOrganization"
            @checked="handleTableCheckedChange"
          />
        </p-tab-pane>
      </p-tabs>

      <TitleSecondary
        title="操作日志"
        :icon="iconStaus"
        @change="handleChangeIcon"
      />
      <ProgressPortrait
        v-show="isShowProgressPortrait"
        :operation-log="operationLogInformation"
      />
      <BottomLine />
    </div>
    <ButtonGroup
      class="g-drawer-footer"
      :options="footerButtons"
      @click="handleButtonClick"
    />
  </p-drawer>
</template>

<script>
import FormBasicInfoList from '@/components/task/FormBasicInfoList.vue';
import TableSaleList from '@/components/task/TableSaleList.vue';
import ProgressTransverse from '@/components/task/ProgressTransverse.vue';
import ProgressPortrait from '@/components/task/ProgressPortrait.vue';
import ProgressStaus from '@/components/task/ProgressStaus.vue';
import TablePlanDetail from '@/components/task/TablePlanDetail.vue';

import { getOperationLog, postSaleDirectorConfirm } from '@/api/plan';
import DrawerMixin from './mixin';

export default {
  components: {
    FormBasicInfoList,
    TablePlanDetail,
    TableSaleList,
    ProgressTransverse,
    ProgressPortrait,
    ProgressStaus,
  },

  mixins: [DrawerMixin],

  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    detail: {
      required: true,
      type: Object,
    },
    planId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      operationLogInformation: [], //操作日志请求返回的信息
      iconStaus: 'down', //icon图片up、down的切换
      planDetailList: [],
      endPlanDetailList: [],
    };
  },
  computed: {
    saleGroupOptions() {
      return this.detail.planSales.reduce((pre, cur) => {
        pre[cur.salesGroupName] = cur.id;
        return pre;
      }, {});
    },
    isShowProgressPortrait() {
      return this.iconStaus === 'up';
    },
    form() {
      return this.detail.plan || {};
    },
    footerButtons() {
      return [
        {
          key: 'close',
          label: '返回',
        },
      ];
    },
  },
  methods: {
    handleTabChange() {},
    async handleChangeIcon(icon) {
      this.iconStaus = icon === 'down' ? 'up' : 'down';
      if (this.operationLogInformation.length === 0) {
        let res = await getOperationLog({
          planNumber: this.planId,
        });
        this.operationLogInformation = res.data.data;
      }
    },
    onClose() {
      this.$emit('update:visible', false);
      this.$emit('fresh');
    },
    closeCallback() {
      this.onClose();
    },

    async saveCallback() {
      await this.submitData(1);
      this.$message.success('保存成功！');
      this.closeCallback();
    },
    async submitCallback() {
      const data = this.$refs.approval?.form || {};
      if (!data.remark) {
        this.$message.error('请先进行审批！');
        return;
      }
      await postSaleDirectorConfirm({ ...data, planId: this.planId });

      this.$message.success('提交成功！');
      this.closeCallback();
    },
    async transferCallback() {
      this.$message.success('transfer');
    },
    handleButtonClick(key) {
      const fn = `${key}Callback`;
      console.log('fn: ', fn);
      this[fn] && this[fn]();
    },
    handleGetStart() {
      this.endPlanDetailList.push(...this.planDetailList);
    },
  },
};
</script>
